package com.guofen.service.yf.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.RdFeedMapper;
import com.guofen.pojo.RdFeed;
import com.guofen.service.yf.RdFeedService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RdFeedServiceImpl implements RdFeedService {
    @Autowired
    private RdFeedMapper rdFeedMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitAllInfo(RdFeed record) {
        int flag = rdFeedMapper.insert(record);
        if(flag > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public RdFeed getlast() {
        return rdFeedMapper.getlast();
    }

    @Override
    public String addFirst(String save,String feedDate, String deviceName_1, String crushStarttime, String crushEndtime, String batch, String zubie, String boxNumber, String incomingSceneMaterial, String crushMaterialWeight, String remark, String recorder) {
        if (save.equals("1")){
            RdFeed rdFeed = new RdFeed();
            rdFeed.setFeedDate(feedDate);
            rdFeed.setDeviceName1(deviceName_1);
            rdFeed.setCrushStarttime(crushStarttime);
            rdFeed.setCrushEndtime(crushEndtime);
            rdFeed.setBatch(batch);
            rdFeed.setZubie(Integer.parseInt(zubie));
            rdFeed.setBoxNumber(Integer.parseInt(boxNumber));
            rdFeed.setIncomingSceneMaterial(incomingSceneMaterial);
            rdFeed.setCrushMaterialWeight(Double.parseDouble(crushMaterialWeight));
            rdFeed.setRemark(remark);
            rdFeed.setRecorder1(recorder);
            int flag = rdFeedMapper.insertSelective(rdFeed);
            if(flag > 0){
                return "保存成功";
            }else {
                return "保存失败";
            }
        }else{
            HashMap<String,Object> map = new HashMap<>();
            map.put("deviceName1",deviceName_1);
            map.put("batch",batch);
            map.put("zubie",Integer.parseInt(zubie));
            map.put("boxNumber",Integer.parseInt(boxNumber));
            int Num = rdFeedMapper.getList3(map).size();
            if (Num == 0){
                RdFeed rdFeed = new RdFeed();
                rdFeed.setFeedDate(feedDate);
                rdFeed.setDeviceName1(deviceName_1);
                rdFeed.setCrushStarttime(crushStarttime);
                rdFeed.setCrushEndtime(crushEndtime);
                rdFeed.setBatch(batch);
                rdFeed.setZubie(Integer.parseInt(zubie));
                rdFeed.setBoxNumber(Integer.parseInt(boxNumber));
                rdFeed.setIncomingSceneMaterial(incomingSceneMaterial);
                rdFeed.setCrushMaterialWeight(Double.parseDouble(crushMaterialWeight));
                rdFeed.setRemark(remark);
                rdFeed.setRecorder1(recorder);
                int flag = rdFeedMapper.insertSelective(rdFeed);
                if(flag > 0){
                    return "保存成功";
                }else {
                    return "保存失败";
                }
            }else {
                return "已保存过是否仍需保存";
            }
        }

    }

    @Override
    public List<RdFeed> getList() {
        return rdFeedMapper.getList();
    }

    @Override
    public boolean addSecond(String belongToId, String deviceName_2, String mixStarttime, String mixEndtime, String remark, String recorder) {
        RdFeed rdFeed1 = rdFeedMapper.selectByPrimaryKey(Integer.parseInt(belongToId));
        String str = rdFeed1.getRemark();
        RdFeed rdFeed = new RdFeed();
        rdFeed.setId(Integer.parseInt(belongToId));
        rdFeed.setMixStarttime(mixStarttime);
        rdFeed.setMixEndtime(mixEndtime);
        rdFeed.setRemark(str+"|"+remark);
        rdFeed.setRecorder2(recorder);
        int flag = rdFeedMapper.updateByPrimaryKeySelective(rdFeed);
        if(flag > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<RdFeed> getList2() {
        return rdFeedMapper.getList2();
    }

    @Override
    public boolean addThird(String belongToId, String deviceName_3, String feedStarttime, String feedEndtime, String feedWeight, String cultivateBoxWeight, String targetWeight, String insectResidusBoxWeight, String insectResidusWeight, String remark, String recorder) {
        RdFeed rdFeed1 = rdFeedMapper.selectByPrimaryKey(Integer.parseInt(belongToId));
        String str = rdFeed1.getRemark();
        RdFeed rdFeed = new RdFeed();
        rdFeed.setId(Integer.parseInt(belongToId));
        rdFeed.setFeedStarttime(feedStarttime);
        rdFeed.setFeedEndtime(feedEndtime);
        rdFeed.setFeedWeight(Double.parseDouble(feedWeight));
        rdFeed.setBoxWeight(Double.parseDouble(cultivateBoxWeight));
        rdFeed.setTargetWeight(Double.parseDouble(targetWeight));
        rdFeed.setInsectResidusBoxWeight(Double.parseDouble(insectResidusBoxWeight));
        rdFeed.setInsectResidusWeight(Double.parseDouble(insectResidusWeight));
        rdFeed.setRemark(str+"|"+remark);
        rdFeed.setRecorder3(recorder);
        int flag = rdFeedMapper.updateByPrimaryKeySelective(rdFeed);
        if(flag > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<RdFeed> list = rdFeedMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<RdFeed> list = rdFeedMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return rdFeedMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(RdFeed record) {
        return rdFeedMapper.updateByPrimaryKeySelective(record);
    }
}
